package app.netant.book.txt65;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class Txt65Detail extends NetAntBase {
	
	private Txt65Entity entity;

	public Txt65Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(Txt65Entity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		
		Elements activePart = document.select("#wrapper .location a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select("#wrapper .left .txtdetail h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text().trim();
			if(title.endsWith("TXT全集下载")){
				title = title.substring(0,title.length()-7);
			}
			
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title);
			
		}
		
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#wrapper .left .txtdetail .info .detail p");
		for(int i=0;i<activePart.size();i++){
			String tag = activePart.get(i).text();
			int idx = tag.indexOf("：");
			if(idx > 0){
				String tagName = tag.substring(0,idx);
				String tagValue = tag.substring(idx+1);
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#wrapper .left .novelcon p");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select("#wrapper .left .down a.downButton");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			if(href.startsWith("/")){
				href = "http://www.65txt.com" + href;
			}
			System.out.println("【Href】:"+href);
			this.entity.setDownPath(href);
		}
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		Txt65Detail detail = new Txt65Detail("http://www.65txt.com/txtinfo/19988.html");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseHref();
		
	}
	
}
